package com.zhenyi.toupiao.interceptor;

import org.hashids.Hashids;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.toupiao.common.model.TpActivity;

public class CountInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		try {
			Controller controller = inv.getController();
			String code = controller.getPara(0);
			if (code == null) {
				code = controller.getPara("activityCode");
			}
			if (code != null) {
				Hashids hashids = new Hashids("toupiao", 4);
				Long activityId = hashids.decode(code)[0];
				TpActivity activity = TpActivity.dao.findById(activityId);
				if (activity != null) {
					if (activity.getVisitCount() == null) {
						activity.setVisitCount(1);
					} else {
						activity.setVisitCount(activity.getVisitCount() + 1);
					}
					activity.update();
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
			LogKit.error("CountInterceptor", e);
		}
		inv.invoke();
	}

}
